package cn.iocoder.yudao.framework.weixin;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
import me.chanjar.weixin.cp.tp.message.WxCpTpMessageRouter;
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceImpl;

/**
 * @Author: lvliangjun
 * @Date: 2024/5/22 18:34
 */
public class T3 {


    //服务商的
    static String providercorpid = "ww00b36ed5acf38dc9";
    static String providerSecret = "gFjBpLXZcwSYCdI6hq2bMxBnlw-v1Xkk0WbTJJVhAAZ1soHfXgF0Owpi-yKl9AgD";

    //模板的
    static String Token = "TPJ4Fdrpasg4R";
    static String encodingAESKey = "msJgW0YBhnDkKQnGz8gtdSFnSarTSIxztBXWxicDPBR";
    //模板ID：dk83899bb2c060ce44
    //模板Secret：iK3sD1n5ovlRTDOl424mu5uGKO_VgXS5oa7V2Yf9UMA
    static String corpid = "dk83899bb2c060ce44";
    static String secret = "iK3sD1n5ovlRTDOl424mu5uGKO_VgXS5oa7V2Yf9UMA";
    public static void main(String[] args) throws WxErrorException {
        String xml = "<xml><ToUserName><![CDATA[dk83899bb2c060ce44]]></ToUserName><Encrypt><![CDATA[vF/mYOM4opSEnplu4ziRWolZKME1R+01kZX6F7h15sVdVyrgVYTaXomFFuhpt4pe9RuFJBn8p/q3cu7r3t+BAytd2Yi+naxk71BmQO1aB/2oqmBHrCtLL0B4X6Rcei487JfZf61WED1cOjF1EnKIVBj+IZka3z6FuZRozvKKcDNV6W9t6zkrwRdvfnRgr/DDHiG++h8RoE0sk3or2rxhPSgHMEnLzt6Zm6JKZr4SNSGCVYhTV9Mkjovz2IVs5QjmDA9BzbN5g6moe8kT4lP84BI8RujeAzR9ofvLCRPOvRHgvS/DouSKy85tzuFCVwgM/GR0FvEUTbLUDlZmL1dDED5VDMUGME0+XBJ/pD8Ewg/vEUWyPkBu0n1T0IAuD9lp]]></Encrypt><AgentID><![CDATA[]]></AgentID></xml>";

         String timestamp="1716437429";
         String nonce="1716097473";
         String msgSignature="67f7cfd9e0c13b8c2f51255a28de5b1eba8ad151";

        WxCpTpServiceImpl baseWxCpService = new WxCpTpServiceImpl();
        WxCpTpDefaultConfigImpl wxCpTpConfigStorage = new WxCpTpDefaultConfigImpl();
        wxCpTpConfigStorage.setCorpId(corpid);
        wxCpTpConfigStorage.setCorpSecret(secret);
        wxCpTpConfigStorage.setToken(Token);
        wxCpTpConfigStorage.setAesKey(encodingAESKey);

        baseWxCpService.setWxCpTpConfigStorage(wxCpTpConfigStorage);



        WxCpTpXmlMessage inMessage = WxCpTpXmlMessage.fromEncryptedXml(xml, baseWxCpService.getWxCpTpConfigStorage(), timestamp, nonce, msgSignature);
        System.out.println(inMessage.getToUserName());
        System.out.println("suiteId:"+inMessage.getSuiteId());
        System.out.println("suiteTicket:"+inMessage.getSuiteTicket());



        WxCpTpMessageRouter wxCpMessageRouter = new WxCpTpMessageRouter(baseWxCpService);
        WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
        if (outMessage != null) {
            System.out.println(outMessage.toEncryptedXml(wxCpTpConfigStorage));
        }
        getSuiteAccessToken(inMessage.getSuiteTicket());

    }
    public static void getSuiteAccessToken(String suiteTicket) throws WxErrorException {
        WxCpTpServiceImpl baseWxCpService = new WxCpTpServiceImpl();
        WxCpTpDefaultConfigImpl wxCpTpConfigStorage = new WxCpTpDefaultConfigImpl();
        wxCpTpConfigStorage.setSuiteId(corpid);
        wxCpTpConfigStorage.setSuiteSecret(secret);


//        wxCpTpConfigStorage.setSuiteTicket("Iw8BmGATuXLo5G4eHmMmpxMFrIuwKRA4No7j2THNT0TQc95dBjsxvQ7TJsnUfVFn");
//        wxCpTpConfigStorage.setSuiteTicketExpiresTime();
        baseWxCpService.setWxCpTpConfigStorage(wxCpTpConfigStorage);
        baseWxCpService.setSuiteTicket(suiteTicket);
        String suiteAccessToken = baseWxCpService.getSuiteAccessToken();
        System.out.println("SuiteAccessToken:"+suiteAccessToken);

    }
}
